﻿using TrueSync;
using Network;

[System.Serializable]
public class WalkForwardState : StateBase
{
   public FP mWalkForwardSpeed;

    public override StateEnum StateName { get { return StateEnum.WalkForward; } }

    LogicMoveComponent mMoveComponent;
    LogicInputComponent mInputComponent;

    public override void Enter(LogicEntity lEntity, StateBase lastState, object[] stateParams)
    {
        base.Enter(lEntity, lastState, stateParams);

        LogicMoveComponent moveComponent = lEntity.GetLogicComponent<LogicMoveComponent>();
        moveComponent.MoveSpeed = new TSVector2(mWalkForwardSpeed * moveComponent.FaceDirection, 0);
        mMoveComponent = lEntity.GetLogicComponent<LogicMoveComponent>();
        mInputComponent = lEntity.GetLogicComponent<LogicInputComponent>();
    }

    public override void Excute(FP deltaTime)
    {
        if (statusComponent.HP <= 0)
        {
            mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Dead);
            return;
        }

        if (statusComponent.Dizzy <= 0)
        {
            mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Dizzy);
            return;
        }

        mMoveComponent.FaceOther();
    }

    public override void Exit()
    {
        mMoveComponent = null;
        mInputComponent = null;
        base.Exit();
    }

    public override void OnInputDirection(int direction)
    {
        switch (direction)
        {
            case 5:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Stand);
                break;
            case 4:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.WalkBackward);
                break;
            case 1:
            case 2:
            case 3:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Crouch);
                break;
            case 8:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Jump);
                break;
            case 7:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.JumpBackward);
                break;
            case 9:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.JumpForward);
                break;

        }
    }

    public override void OnStimulate(Stimulate stimulate)
    {
        LogicStateMachineComponent stateMachineComponent = mLEntity.GetLogicComponent<LogicStateMachineComponent>();
        switch (stimulate.StimulateType)
        {
            case StimulateTypeEnum.FalldownStimulate:
                stateMachineComponent.ChangeState(StateEnum.KnockdownHit);
                break;
            case StimulateTypeEnum.HitFloatStimulate:
                HitFloatStimulate hitfloatStimulate = (HitFloatStimulate)stimulate;
                stateMachineComponent.ChangeState(StateEnum.FloatHit, hitfloatStimulate.XMoveLength, hitfloatStimulate.XMoveSpeed, hitfloatStimulate.YHeight);
                break;
            case StimulateTypeEnum.HitBackStimulate:
                HitBackStimulate hitbackStimulate = (HitBackStimulate)stimulate;
                stateMachineComponent.ChangeState(StateEnum.StandHit, hitbackStimulate.XMoveLength, hitbackStimulate.XMoveSpeed);
                break;
            case StimulateTypeEnum.HitFlyStimulate:
                HitFlyStimulate hitflyStimulate = (HitFlyStimulate)stimulate;
                stateMachineComponent.ChangeState(StateEnum.HitFly, hitflyStimulate.XMoveLength, hitflyStimulate.XMoveSpeed);
                break;
            case StimulateTypeEnum.ThrowStimulate:
                stateMachineComponent.ChangeState(StateEnum.ThrowHit);
                break;
        }
    }

    public override void Serialize(EByte ms)
    {
        base.Serialize(ms);
        ms.WriteLong(mWalkForwardSpeed.RawValue);
    }

    public override void Deserialize(EByte ms)
    {
        base.Deserialize(ms); 
        mWalkForwardSpeed = FP.FromRaw(ms.ReadLong());
    }
}
